

namespace apeng {
    /**进度合并 整理为1个 */
    export class ProgressDir {

        /**当前进度 */
        private ratio: number = 0
        private ratioAll: { [key: string]: number } = {}
        private progressLength: number = 0

        private onProgress: (ratio: number) => void = null!

        constructor(onProgress: (ratio: number) => void) {
            this.onProgress = onProgress
            if (this.onProgress)
                this.onProgress(0)
        }

        public progress(url: string) {
            if (this.ratioAll[url] == undefined)
                this.progressLength++
            let func = (ratio: number, cocosRatio?: number) => {
                //适配cocos加载进度
                if (cocosRatio != undefined)
                    ratio = ratio / cocosRatio

                // 避免多次回调相同得值
                this.ratioAll[url] = ratio
                let _ratio = 0
                for (let key in this.ratioAll)
                    _ratio += this.ratioAll[key]
                this.ratio = _ratio / this.progressLength
                if (this.onProgress)
                    this.onProgress(this.ratio)
            }
            func(0)
            return func
        }

        public getCurRatio() {
            return this.ratio
        }

    }
}